package androidtool.checkconvention;

import java.util.ArrayList;
import java.util.List;
import org.dom4j.Element;

/**
 *
 * @author Hoang
 */
public class WidgetTreeNode {

    private Element rootNode;
    private List<WidgetTreeNode> listChildNode;

    public WidgetTreeNode(Element rootNode) {
        this.rootNode = rootNode;
        this.listChildNode = new ArrayList<>();
        List<Element> listElement = rootNode.elements();
        if (listElement.size() > 0) {
            for (Element element : listElement) {
                listChildNode.add(new WidgetTreeNode(element));
            }
        }
    }

    public Element getRootNode() {
        return rootNode;
    }

    public List<WidgetTreeNode> getListChildNode() {
        return listChildNode;
    }

    public List<Widget> getAllWidgetHaveId() {
        List<Widget> result = new ArrayList<>();
        List<WidgetTreeNode> listWidgetTreeNode = new ArrayList<>();
        preOrder(this, listWidgetTreeNode);
        for (WidgetTreeNode widgetTreeNode : listWidgetTreeNode) {
            Widget widget = new Widget(widgetTreeNode.getRootNode());
            if (widget.getId() != null && !widget.getType().equals("include")) {
                result.add(widget);
            }
        }
        return result;
    }

    public List<Widget> getAllWidget() {
        List<Widget> result = new ArrayList<>();
        List<WidgetTreeNode> listWidgetTreeNode = new ArrayList<>();
        preOrder(this, listWidgetTreeNode);
        for (WidgetTreeNode widgetTreeNode : listWidgetTreeNode) {
            result.add(new Widget(widgetTreeNode.getRootNode()));
        }
        return result;
    }

    private void preOrder(WidgetTreeNode treeNode, List<WidgetTreeNode> result) {
        if (treeNode != null) {
            result.add(treeNode);
            for (WidgetTreeNode widgetTreeNode : treeNode.getListChildNode()) {
                preOrder(widgetTreeNode, result);
            }
        }
    }
}
